<?php
 
class Controller_Action extends Zend_Controller_Action 
{
    protected $_flexyElements  = array();
    protected $_templateFile   = null;
    protected $_masterTemplate = 'main.html';
    
    /**
     * @var Application_Registry_Request
     */
    protected $_request        = null;
    
    /**
     * @var Application_Registry_Session
     */
    protected $_session        = null;
    
    public function indexAction() {}
    
    public function noRouteAction() {
        $this->_redirect('/home');
    }
    
    public function __construct() {
        $this->_request = Application_Registry_Request::getInstance();
        $this->_session = Application_Registry_Session::getInstance();
        if ($this->_session->loggedUser) {
            $this->username = $this->_session->loggedUser->getName();
        }
    }
    
    public function __destruct() {
        $this->_addRequestToObject();
        $this->_output();
    }
    
    protected function _output() {
        $this->_prepareTemplateFilename();
        $this->_prepareMenu();
        $this->access   = Application_Auth::getInstance()->isReachable($this->_action) ? 'OK':'NOT';

        $templateEngine = Application_Helper::getInstance()->getTemplateEngine();
        $templateEngine->compile($this->_masterTemplate);
        $templateEngine->outputObject($this,$this->_flexyElements);
    }
    
    protected function _prepareTemplateFilename() {
        if (isset($this->_templateFile)) {
            $this->mainTemplate = $this->_templateFile;
        } elseif (substr($this->_action->getActionName(), 0, 4) == 'AJAX') {
            $this->_masterTemplate = 'common/AJAX.html';
        } else {
            $retVal = ucfirst($this->_action->getControllerName());
            if ($this->_action->getActionName() != 'index') {
                $retVal .= '/'.ucfirst($this->_action->getActionName());
            }
            $this->mainTemplate = $retVal.'.html';
        }
        //echo $this->mainTemplate;
    }
    
    public function showMiddle() {
        $flexy = new HTML_Template_Flexy();
        $flexy->compile($this->mainTemplate);
        return $flexy->bufferedOutputObject($this, $this->_flexyElements);
    }
    
    protected function _prepareMenu() {
    }
    
    protected function _addRequestToObject() {
        $params = $this->_getAllParams();
        foreach ($params as $key => $value) {
            $this->$key = $value;
        }
    }
    
}
 
?>